const router = require('koa-router')()
const { query } = require('../db/dbmanager');
const db = require('../db/initdb')
const path = require("path")
const fs = require("fs")
const { koaBody } = require('koa-body')
router.prefix('/users')
let t = new Date()
let time_str = `${t.getFullYear()}-${t.getMonth() + 1}-${t.getDate()}`
router.get('/', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})
router.post('/u', async (ctx, next) => {
  console.log(ctx.request.body)
  let user = ctx.request.body

  let sql = `INSERT INTO hs.user (id, name,avatar,relogin,concernnum) VALUES ($1, $2, $3,$4,1) RETURNING id`
  let sql2 = `INSERT INTO hs.concern (userid,concernid,hu) VALUES ($1,5,2)`
  // let t = new Date()
  // let time_str = `${t.getFullYear()}-${t.getMonth() + 1}-${t.getDate()}`
  let ret = await db.query(sql, [Math.random().toString(16).substring(2, 14), user.nickName, user.avatarUrl, time_str])
  console.log(ret)
  let ret2 = await db.query(sql2, [ret.rows[0].id])
  ctx.body = ret.rows[0]
})


router.post('/api/addimg', koaBody(), async (ctx, next) => {
  console.log(ctx.request.query.data)
  let img = (ctx.request.query.img)
  let id = (ctx.request.query.data)
  let name = (ctx.request.files.image.newFilename)
  let sql2;
  if (img) {
    sql2 = `UPDATE hs.picaddress set perface=$1 where picid=$2 returning picid`
  } else {
    sql2 = `INSERT INTO hs.picaddress`
      + `(address, workid)`
      + ` VALUES($1, $2) returning picid`;
  }
  let ret2 = await db.query(sql2, [name, id])
  ctx.body = ret2.rows[0].picid
});
router.post('/api/addimgpup', koaBody(), async (ctx, next) => {
  console.log(ctx.request.query.img)
  let id = (ctx.request.query.data)
  let img = (ctx.request.query.img)
  let name = (ctx.request.files.image.newFilename)
  let sql2;
  if (img == '0') {
    sql2 = `UPDATE hs.card SET image=$1 where id=$2`
  }
  else {
    sql2 = `UPDATE hs.card SET perface=$1 where id=$2`
  }
  let ret2 = await db.query(sql2, [name, id])
  ctx.body = name
});
router.post('/vaddimg', koaBody(), async (ctx, next) => {
  console.log(ctx.request.files.file.newFilename)
  ctx.body = ctx.request.files.file.newFilename
})
router.post('/api/publish', async (ctx, next) => {
  console.log(ctx.request.body)

  let data = ctx.request.body
  let sql = `INSERT INTO hs.works`
    + `(userid, picture, title, detail,date,topic,person,local,much,tu)`
    + ` VALUES($1, $2, $3, $4,$5,$6,$7,$8,$9,$10) RETURNING id`;

  let args = [
    data.userid,
    !data.picture,
    data.title,
    data.detail,
    time_str,
    data.topic,
    data.person,
    data.location,
    data.much,
    data.tu
  ]

  let ret = await db.query(sql, args)
  console.log(ret.rows[0].id)
  let id = ret.rows[0].id
  ctx.body = id
});
router.post('/api/pup', async (ctx, next) => {
  let data = ctx.request.body
  let sql = `INSERT INTO hs.card`
    + `(userid, name, outline,type,date,image)`
    + ` VALUES($1, $2, $3,$4,$5,$6) RETURNING id`;

  let args = [
    data.userid,
    data.name,
    data.outline,
    data.type,
    new Date(),
    data.image
  ]

  let ret = await db.query(sql, args)
  console.log(ret.rows[0].id)
  let id = ret.rows[0].id
  ctx.body = id
});
router.post('/voice', async (ctx, next) => {
  let work = (ctx.request.query)
  let sql = `INSERT INTO hs.picaddress`
    + `(address, workid)`
    + ` VALUES($1, $2) returning picid`;
  let ret = await db.query(sql, [work.name, work.data])
  ctx.body = ret.rows[0].picid
})
router.get('/detail', async (ctx, next) => {

  let data = ctx.request.query.workid;
  console.log(data)
  let sql = `DELETE FROM hs.works where id=$1`
  let ret = await db.query(sql, [data])
  ctx.body = ret.rows[0]
})
router.post('/edit', async (ctx, next) => {
  console.log(ctx.request.body)
  let body = ctx.request.body
  let sql = `UPDATE hs.manager SET avatar=$1,pwd=$3,name=$4 where id=$2 returning *`
  let ret = await db.query(sql, [body.avatar, body.id, body.pwd, body.name])
  ctx.body = ret.rows[0]
})
router.post('/login', async (ctx, next) => {
  console.log(ctx.request.body)
  let name = ctx.request.body.userName
  let pwd = ctx.request.body.password
  let sql = `select * from hs.manager where name=$1 and pwd=$2`
  let ret = await db.query(sql, [name, pwd])
  console.log(name, pwd, ret.rows[0])
  if (ret.rows[0]) {

    ctx.body = { code: 200, data: ret.rows[0] }
  }
  else {
    ctx.body = { code: 404 }
  }
})
module.exports = router
